package com.baseproject.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baseproject.mvp.MvpFragment;

/**
 * Created by hp on 2017/6/3.
 */

public abstract class BaseItemViewDelegate<T> implements ItemViewDelegate<T> {

    protected ViewHolder mViewHolder;
    protected MvpFragment mFragment;
    protected T mData;


    /**
     * 0 数据未初始化
     * 1 数据初始化完成但是未空
     * 2 数据初始化完成且有效
     */
    protected int mDataStatus = 0;

    protected RecyclerView.LayoutParams mLayoutParamsBackup;

    public BaseItemViewDelegate() {
    }

    public BaseItemViewDelegate(MvpFragment mFragment) {
        this.mFragment = mFragment;
    }

    @Override
    public ViewHolder createViewHolder(Context context, ViewGroup parent) {
        View itemView = LayoutInflater.from(context).inflate(getItemViewLayoutId(), parent, false);
        mViewHolder = new ViewHolder(context, itemView);
        _createViewHolder(context, parent);
        if (mDataStatus == 1) {
            getDataOkButDataIsEmpty(mViewHolder, mData);
        } else if (mDataStatus == 2) {
            updateValidDataAfterCreate(mViewHolder, mData);
        }
        return mViewHolder;
    }

    @Override
    public void convert(ViewHolder holder, int position) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public void updateData(T t) {
        if (null == t) {
            mDataStatus = 1;
            if (null != mViewHolder) {
                getDataOkButDataIsEmpty(mViewHolder, mData);
            }
        } else {
            mData = t;
            mDataStatus = 2;
            if (null != mViewHolder) {
                updateValidDataAfterCreate(mViewHolder, mData);
            }
        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    /**
     * 有时会出现调用updateData太快，导致尚未走onCreateViewHolder，很多变量未初始化，因此需要在onCreateViewHolder和updateData时都进行调用
     * 调用时mData已经更新赋值完毕
     */
    abstract protected void updateValidDataAfterCreate(ViewHolder viewHolder, T data);

    /**
     * 正常获取到了数据， 只不过数据确实不存在(例如还没有向服务器提交最近阅读记录)
     */
    abstract protected void getDataOkButDataIsEmpty(ViewHolder viewHolder, T data);

    abstract protected void _createViewHolder(Context context, ViewGroup parent);


    public void hideItem(ViewHolder viewHolder) {
        RecyclerView.LayoutParams param = (RecyclerView.LayoutParams) viewHolder.itemView.getLayoutParams();
        mLayoutParamsBackup = new RecyclerView.LayoutParams(param);
        viewHolder.itemView.setVisibility(View.GONE);
        param.height = 0;
        param.width = 0;
        viewHolder.itemView.setLayoutParams(param);
    }

    public void showItem(ViewHolder viewHolder) {
        RecyclerView.LayoutParams param = (RecyclerView.LayoutParams) viewHolder.itemView.getLayoutParams();
        if (null != mLayoutParamsBackup) {//之前隐藏过
            param.height =mLayoutParamsBackup.height;
            param.width = mLayoutParamsBackup.width;
            viewHolder.itemView.setLayoutParams(param);
        } else {//之前没隐藏过
//            param.height = LinearLayout.LayoutParams.WRAP_CONTENT;
//            param.width = LinearLayout.LayoutParams.MATCH_PARENT;
        }
        viewHolder.itemView.setVisibility(View.VISIBLE);
    }
}
